package com.lx.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

/**
 * @author c先生
 * @date 2022年04月02日 下午 2:46:50
 * @describe
 */

public class test {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        //创建音乐目录以及假数据
        //存储音乐的目录
        File file = new File("D:/music");
        //files所有的音乐
        File[] files = file.listFiles();
        for (File file1 : files) {
            System.out.println(file1.getName());
        }
        System.out.println("请输入下载的音乐名");
        String inputName = sc.next();  // 经济舱
        String downLoadName = null;
        for (File file1 : files) {
            if(file1.getName().indexOf(inputName)!=-1){
                downLoadName=file1.getName();
                break;
            }
        }
        if(downLoadName!=null){
            //用户输入的音乐存在
            System.out.println("请收入下载的路径");
            String downLoadPath = sc.next();
            //创建目录
            File newMusicDir = new File(downLoadPath);
            if(!newMusicDir.exists()){
                newMusicDir.mkdirs();
            }
            File downLoadFile = new File(file,downLoadName);//D:/music/downLoadName
            FileInputStream fis = new FileInputStream(downLoadFile);
            File newMusicFile = new File(newMusicDir,downLoadName);//..../downLoadName
            FileOutputStream fos = new FileOutputStream(newMusicFile);
            byte bs  [] = new byte[4096];
            int len;
            while((len=fis.read(bs))!=-1){
                fos.write(bs,0,len);
            }
            fis.close();
            fos.close();
            System.out.println("下载成功");
        }else{
            System.out.println("不存在此音乐，无法下载");
        }
    }
}
